﻿using System;
using System.Linq;
using Loki.Commands;
using Loki.UI;
using Loki.Utils;
using Luna.Model.CRM;
using Luna.UI.Resources;

namespace Luna.UI.CRM
{
    public class CategoryListViewModel : LunaViewModel
    {
        public LokiCollection<DisplayableCategory> Categories { get; private set; }

        public CategoryListViewModel()
        {
            Categories = CreateTrackedCollection<DisplayableCategory>();

            DisplayName = FormNames.CATEGORY_LIST;
            GroupName = FormNames.GROUP_CRM;

            Init += CategoryListViewModel_Init;
            Load += CategoryListViewModel_Load;
        }

        private void CategoryListViewModel_Load(object sender, EventArgs e)
        {
            LoadCategories();
        }

        private void CategoryListViewModel_Init(object sender, EventArgs e)
        {
            Commands.Handle(Loki.Commands.ApplicationCommands.SAVE, ApplicationSave_CanExecute, ApplicationSave_Execute);
        }

        private void LoadCategories()
        {
            Categories.BeginInit();
            Categories.Clear();

            foreach (var source in ApplicationModel.CRM.Categories.OrderBy(x => x.Name).Select(x => x.EditableCopy))
            {
                var display = new DisplayableCategory() { Category = source };
                display.LoadValues();
                Categories.Add(display);
            }

            Categories.EndInit();
            AcceptChanges();
        }

        private void ApplicationSave_CanExecute(object sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= IsChanged;
        }

        private void ApplicationSave_Execute(object sender, CommandEventArgs e)
        {
            Validate();
            if (IsValid)
            {
                foreach (var modifiedCategory in Categories)
                {
                    IEditableCategory category = modifiedCategory.Category;
                    if (modifiedCategory.Category == null)
                    {
                        modifiedCategory.Category = ApplicationModel.Create<IEditableCategory>();
                    }

                    modifiedCategory.CollectValues();

                    ApplicationModel.CRM.Save(modifiedCategory.Category);
                }

                foreach (var deletedCategory in Categories.RemovedItems)
                {
                    if (deletedCategory.Category != null)
                    {
                        ApplicationModel.CRM.Delete(deletedCategory.Category);
                    }
                }

                LoadCategories();
            }
            else
            {
                UserWarning(Error);
            }
        }

        public override bool Validate(ValidationMode mode)
        {
            bool buffer = base.Validate(mode);
            foreach (var category in Categories)
            {
                buffer &= category.Validate(mode);
            }

            return buffer;
        }
    }
}